/**
 * 集群模块控制器
 * 
 * @author zhongfuhua
 */

var jobGroupApp = angular.module("jobGroupApp");

jobGroupApp.controller("jobGroupRetryController", [
		"$scope",
		"commonService",
		"jobDetailService",
		"jobDetailTrackService",
		"$timeout",
		"$log",
		function($scope, commonService, jobDetailService, jobDetailTrackService, $timeout, $log) {
			
			// 表格
			$scope.$jobGroupRetryTable = $('#jobGroupRetryTable');
			$scope.$jobGroupRetryModal = $('#jobGroupRetryModal');
			
			$scope.jobDetailRetryVO = {
				jobGroupName : null,
				clusterName : null
			};
			
			$scope.jobGroupRetryTrackTableCurrent = null;
			
			// 监视isSucce的状态
			$scope.action = null;
			$scope.isSuccess = true;
			$scope.error = null;
			
			ffc.util.watchResult($scope, $timeout);
			
			/**
			 * 
			 * 
			 * 
			 */
			$scope.publicVO.jobDetailRetryInit = function(row){
				$scope.jobDetailRetryVO = {};
				$scope.jobDetailRetryVO.jobGroupName = row.name;
				$scope.jobDetailRetryVO.clusterName = row.clusterName;
				ffc.util.angularApply($scope, $timeout);
				$scope.queryJobDetailRetry();
				$scope.$jobGroupRetryModal.modal("show");
			}
			
			
			/**
			 * 
			 * 功能说明:查询待执行任务
			 * 
			 */
			$scope.queryJobDetailRetry = function(){
				$scope.$jobGroupRetryTable.bootstrapTable("selectPage", 1);  
			}
			
			
			
			$(function() {
				
				/**
				 * 
				 * 功能说明:初始化数据源表格
				 * 
				 */
				$scope.$jobGroupRetryTable.bootstrapTable({
					queryParams : function(params) {
						var selfParams = {
							jobGroupName : $scope.jobDetailRetryVO.jobGroupName,
							clusterName : $scope.jobDetailRetryVO.clusterName
						}
						return $.extend({}, params, selfParams);
					},
					ajax : function(render) {
						jobDetailService.queryRetryPage(render.data, function(result) {
							if(!result.result){
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							ffc.util.loadTableData(render, result);  
						},function(error){
							$scope.error = error;
							$scope.isSuccess = false;
						});
					},
					onExpandRow : function(index, row, $detail){
						var jobGroupRetryTrackTable = $detail.html(
								"<div class=\"table-responsive\">" +
									"<table name='jobGroupRetryTrackTable'></table>" +
								"</div>"
								).find("table");
						
						var columns = [];
						
						
						columns.push({
							field : "opaque",
							title : "opaque",
							width : "5%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "addr",
							title : "地址",
							width : "35%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "fireTime",
							title : "触发时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "sendTime",
							title : "发送时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "reciveTime",
							title : "执行端接收时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "completeTime",
							title : "完成时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						$scope.jobGroupRetryTrackTableCurrent = jobGroupRetryTrackTable;
						
						jobGroupRetryTrackTable.bootstrapTable({
							idField : "id",
							sidePagination: "server",
							columns : columns,
							pagination : true,
							pageList : [5, 10, 20],
							detailView : false,
							queryParamsType : "",
							queryParams : function(params) {
								var selfParams = {
									id : row.id,
									jobName : row.jobName,
									clusterName : row.clusterName
								};
								return $.extend({}, params, selfParams);
							},
							ajax : function(render) {
								jobDetailTrackService.queryPage(render.data, function(result) {
									if(!result.result){
										$scope.error = result.msgTitle;
										$scope.isSuccess = false;
										return false;
									}
									ffc.util.loadTableData(render, result);  
								},function(error){
									$scope.error = error;
									$scope.isSuccess = false;
								});
							}
						});
					}
				});
				
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 */
				$scope.$jobGroupRetryTable.on('load-success.bs.table', function (data) {
					$('[data-toggle="popover"]').popover();
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 */
				$scope.$jobGroupRetryModal.on("hidden.bs.modal",function(){
					$scope.jobDetailRetryVO = {};
				});
				
				
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 */
				statusFormatter = function(value, row, index) {
					if(value == 'FIRE'){
						return "开始";
					}else if(value == 'SEND'){
						return "已发送"
					}else if(value == 'RECIVE'){
						return "送达"
					}else if(value == 'COMPLETE'){
						return "完成"
					}else if(value == 'OFFLINE'){
						return "离线"
					}else if(value == 'TIMEOUT'){
						return "超时"
					}else if(value == 'OFFLINE_TIMEOUT'){
						return "离线超时"
					}
					return value;
				}
				
				
			});
}]);
